<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $partySelected        = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : '' ;  
  $status               = isset($_REQUEST['status']) ? $_REQUEST['status'] : '' ;  
	$msg                  = 'Record Not Found...!';
  $deliveryChallanArray = array();
  $deliveryDetailArray  = array();
  $deliveryChallanCount = 0;
  $pendingDCArray       = array();
  $pendingDCDetailArray = array();
  $pendingDCCount       = 0;
  $masterDeliveryId     = 0;
  
  /////////////////  Final Master Date : Start
	if(isset($_REQUEST['fromDateMonth']))
		$fromDate  = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-01";
	else
		$fromDate  = date("Y-m-d");
	
	if(isset($_REQUEST['toDateMonth']))
		$toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-31";
	else
		$toDate    = date("Y-m-d");
	/////////////////  Final Master Date : Stop  

  if(isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != 0)
  {
  	$ifParty = " deliverychallanmaster.partyId = ".$_REQUEST['partyId']." ";
  }
  else
  {
  	$ifParty = " 1 = 1 ";
  }
  
  if(isset($_REQUEST['toDateMonth']) && $_REQUEST['toDateMonth'] != '')
  {
  	$selFinalMaster = "SELECT *,DATE_FORMAT(dateMaster,'%d-%m-%Y') AS fmDate 
  	                     FROM masterdelivery 
  	                    WHERE dateMaster >= '".$fromDate."'
                          AND dateMaster <= '".$toDate."'
                          AND isProforCreated = 'N' ";
    if(isset($_REQUEST['partyId']) && $_REQUEST['partyId'] > 0)
    {
      $selFinalMaster .= " AND partyId =".$_REQUEST['partyId']." ";
    }
      
    $selFMRes = mysql_query($selFinalMaster);
    while($fmRow = mysql_fetch_array($selFMRes))
    {
    	$deliveryChallanArray[$deliveryChallanCount]['masterDeliveryId']  = $fmRow['masterDeliveryId'];
    	$deliveryChallanArray[$deliveryChallanCount]['fmDate']            = $fmRow['fmDate'];
    	
    	$selMstDtl = "SELECT deliverychallandetail.discription, deliverychallandetail.quantity,deliverychallanmaster.srNo,
    	                     DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,deliverychallanmaster.challanType,
    	                     deliverychallanmaster.deliveryChallanMasterId,party.companyName,item.itemName,subcategory.subCategoryName
    	                FROM deliverychallandetail
    	                JOIN deliverychallanmaster ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
    	                JOIN party ON deliverychallanmaster.partyId = party.partyId
    	                JOIN item ON item.itemId = deliverychallandetail.itemId
                      JOIN subcategory ON subcategory.subCategoryId = deliverychallandetail.subCategoryId
    	               WHERE masterDeliveryId = ".$fmRow['masterDeliveryId']."
    	               ORDER BY deliverychallanmaster.deliveryChallanMasterId";
      $selMstDtlRes = mysql_query($selMstDtl);
      $deliveryDetailCount = 0;
      while($mstDtlRow = mysql_fetch_array($selMstDtlRes))
      {
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['masterDeliveryId']  = $fmRow['masterDeliveryId'];
      	if(isset($deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount-1]['masterDeliveryId']) && $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['masterDeliveryId'] == $fmRow['masterDeliveryId'])
      	{
      		$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['masterDeliveryId'] = "";
      		$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['display'] = 0;
      	}
      	else
      	{
      		$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['masterDeliveryId'] = $fmRow['masterDeliveryId'];
      		$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['display'] = 1;
      	}
      	
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['companyName']  = $mstDtlRow['companyName'];
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['srNo']         = $mstDtlRow['srNo'];
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['delivaryDate'] = $mstDtlRow['delivaryDate'];
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['challanType']  = $mstDtlRow['challanType'];
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['subCategoryName'] = $mstDtlRow['subCategoryName'];
      	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['quantity']        = $mstDtlRow['quantity'];
  	    if($mstDtlRow['discription'] != "")
  	    {
  	      $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['itemName'] = $mstDtlRow['itemName']." # ".$mstDtlRow['discription'];
  	    }                                                                               
  	    else                                                                            
  	    {                                                                               
  	      $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['itemName'] = $mstDtlRow['itemName'];
  	    }
  	    $deliveryDetailCount++;
      }
      $deliveryChallanCount++;
    }
                              
//    // Selection of data when final master created : Starts
//    $selectDeliveryChallan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,
//                                     deliverychallanmaster.challanType, deliverychallanmaster.srNo,
//                                     deliverychallanmaster.status,user.userId,user.userName,
//                                     DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
//                                     party.partyId,party.companyName
//                                FROM deliverychallanmaster
//                                JOIN user  ON user.userId   = deliverychallanmaster.userId
//                                JOIN party ON party.partyId = deliverychallanmaster.partyId
//                               WHERE ".$ifParty."
//                                 AND deliverychallanmaster.delivaryDate >= '".$fromDate."'
//                                 AND deliverychallanmaster.delivaryDate <= '".$toDate."'
//                                 AND deliverychallanmaster.status = 'FM'
//                               ORDER BY party.partyId";                  
//    $selectDeliveryResult = mysql_query($selectDeliveryChallan);
//	  while($deliveryChallanRow = mysql_fetch_array($selectDeliveryResult))
//	  {
//	    $deliveryChallanArray[$deliveryChallanCount]['deliveryChallanMasterId']  = $deliveryChallanRow['deliveryChallanMasterId'];
//	    $deliveryChallanArray[$deliveryChallanCount]['companyName']              = $deliveryChallanRow['companyName'];
//	    $deliveryChallanArray[$deliveryChallanCount]['srNo']                     = $deliveryChallanRow['srNo'];
//	    $deliveryChallanArray[$deliveryChallanCount]['challanType']              = $deliveryChallanRow['challanType'];
//	    $deliveryChallanArray[$deliveryChallanCount]['display']                  = 0;
//	    $selectdeliverychallandetail = "SELECT deliverychallandetail.deliveryChallanMasterId,deliverychallandetail.deliveryChallanDetailId,deliverychallandetail.discription,
//	                                           deliverychallandetail.itemId,deliverychallandetail.subCategoryId,deliverychallandetail.quantity,
//	                                           deliverychallandetail.masterDeliveryId,item.itemId,item.itemName,subcategory.subCategoryId,subcategory.subCategoryName,
//	                                           DATE_FORMAT(masterdelivery.dateMaster,'%d-%m-%Y') AS dateMaster
//                                        FROM deliverychallandetail
//                                        JOIN item ON item.itemId = deliverychallandetail.itemId
//                                        JOIN subcategory ON subcategory.subCategoryId = deliverychallandetail.subCategoryId
//                                        LEFT JOIN masterdelivery ON deliverychallandetail.masterDeliveryId = masterdelivery.masterDeliveryId
//                                       WHERE deliverychallandetail.masterDeliveryId != 0
//                                         AND deliverychallandetail.isProforCreated = 'N'
//                                         AND deliverychallandetail.deliveryChallanMasterId = ".$deliveryChallanRow['deliveryChallanMasterId']."
//                                       ORDER BY dateMaster";  
//      $deliverychallandetailQuery = mysql_query($selectdeliverychallandetail);    
//      $deliveryDetailCount = 0;     
//      while($deliverychallandetailRow = mysql_fetch_array($deliverychallandetailQuery))
//      {
//      	$deliveryChallanArray[$deliveryChallanCount]['dateMaster'] = $deliverychallandetailRow['dateMaster'];
//      	$deliveryChallanArray[$deliveryChallanCount]['display'] = 1;
//  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['deliveryChallanDetailId'] = $deliverychallandetailRow['deliveryChallanDetailId'];
//  	    if($deliverychallandetailRow['discription'] != "")
//  	    {
//  	      $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['itemName'] = $deliverychallandetailRow['itemName']." # ".$deliverychallandetailRow['discription'];
//  	    }                                                                               
//  	    else                                                                            
//  	    {                                                                               
//  	      $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['itemName'] = $deliverychallandetailRow['itemName'];
//  	    }
//  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['subCategoryName']         = $deliverychallandetailRow['subCategoryName'];
//  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['quantity']                = $deliverychallandetailRow['quantity'];
//  	    $masterDeliveryId                                                                            = $deliverychallandetailRow['masterDeliveryId'];
//  	    $deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['rowCount']                = $deliveryDetailCount;
//        
//        echo "<br>Pre :".$deliveryDetailArray[$deliveryChallanCount-1][$deliveryDetailCount]['masterDeliveryId'];
//  	    echo "<br>Cur :".$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['masterDeliveryId']        = $deliverychallandetailRow['masterDeliveryId'];
//  	    
//  	    
//        if(isset($deliveryDetailArray[$deliveryChallanCount-1][$deliveryDetailCount]['masterDeliveryId']) && 
//           $deliveryDetailArray[$deliveryChallanCount-1][$deliveryDetailCount]['masterDeliveryId'] == $deliverychallandetailRow['masterDeliveryId'])
//        {
//        	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['sameFM'] = 1;
//        }
//        else
//        {
//        	$deliveryDetailArray[$deliveryChallanCount][$deliveryDetailCount]['sameFM'] = 0;
//        }	
//
//  	    $deliveryDetailCount++;
//      }
//	    $deliveryChallanCount++;
//	  }
//    // Selection of data when final master created : Ends
//    
    // Selection of pending details data : Starts
    $recordPresent = 0;
    $selectDCPending = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                               deliverychallanmaster.srNo,deliverychallanmaster.status,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                               party.partyId,party.companyName
                          FROM deliverychallanmaster
                          JOIN user  ON user.userId   = deliverychallanmaster.userId
                          JOIN party ON party.partyId = deliverychallanmaster.partyId
                         WHERE ".$ifParty."
                           AND deliverychallanmaster.delivaryDate >= '".$fromDate."'
                           AND deliverychallanmaster.delivaryDate <= '".$toDate."'
                           AND ( deliverychallanmaster.status = 'A' OR deliverychallanmaster.status = 'FM')";
    $selectDCPendingResult = mysql_query($selectDCPending);
	  while($pendingDCRow = mysql_fetch_array($selectDCPendingResult))
	  {
	    $pendingDCArray[$pendingDCCount]['deliveryChallanMasterId'] = $pendingDCRow['deliveryChallanMasterId'];
	    $pendingDCArray[$pendingDCCount]['companyName']             = $pendingDCRow['companyName'];
	    $pendingDCArray[$pendingDCCount]['srNo']                    = $pendingDCRow['srNo'];
	    $pendingDCArray[$pendingDCCount]['challanType']             = $pendingDCRow['challanType'];
	    $pendingDCArray[$pendingDCCount]['display'] = 0;
	    $selectdeliverychallandetail = "SELECT deliverychallandetail.deliveryChallanMasterId,deliverychallandetail.deliveryChallanDetailId,deliverychallandetail.discription,
	                                           deliverychallandetail.itemId,deliverychallandetail.subCategoryId,deliverychallandetail.quantity,
	                                           deliverychallandetail.masterDeliveryId,item.itemId,item.itemName,subcategory.subCategoryId,subcategory.subCategoryName
                                        FROM deliverychallandetail
                                        JOIN item        ON item.itemId = deliverychallandetail.itemId
                                        JOIN subcategory ON subcategory.subCategoryId = deliverychallandetail.subCategoryId
                                       WHERE deliverychallandetail.masterDeliveryId = 0
                                        AND deliverychallandetail.deliveryChallanMasterId = ".$pendingDCRow['deliveryChallanMasterId']."
                                        AND deliverychallandetail.isFinalCreated = 'N' "; 
      $deliverychallandetailQuery = mysql_query($selectdeliverychallandetail);    
      $pendingDCDetailCount = 0;     
      while($pendingDCDetailRow = mysql_fetch_array($deliverychallandetailQuery))
      {
      	$pendingDCArray[$pendingDCCount]['display'] = 1;
      	$pendingDCDetailArray[$pendingDCCount][$pendingDCDetailCount]['deliveryChallanDetailId'] = $pendingDCDetailRow['deliveryChallanDetailId'];
  	    if($pendingDCDetailRow['discription'] != "")
  	    {
  	      $pendingDCDetailArray[$pendingDCCount][$pendingDCDetailCount]['itemName'] = $pendingDCDetailRow['itemName']." # ".$pendingDCDetailRow['discription'];
  	    }                                                                           
  	    else                                                                        
  	    {                                                                           
  	      $pendingDCDetailArray[$pendingDCCount][$pendingDCDetailCount]['itemName'] = $pendingDCDetailRow['itemName'];  
  	    }
  	    
  	    $pendingDCDetailArray[$pendingDCCount][$pendingDCDetailCount]['subCategoryName']         = $pendingDCDetailRow['subCategoryName'];
  	    $pendingDCDetailArray[$pendingDCCount][$pendingDCDetailCount]['quantity']                = $pendingDCDetailRow['quantity'];
  	    $pendingDCDetailArray[$pendingDCCount][$pendingDCDetailCount]['rowCount']                = $pendingDCDetailCount;
  	    $pendingDCDetailCount++;
      }
	    $pendingDCCount++;
	  }
    // Selection of pending details data : Ends
  }
  include("./bottom.php");
  $smarty->assign("status",$status);
  $smarty->assign("partySelected",$partySelected);
  $smarty->assign("msg",$msg);
  $smarty->assign("deliveryChallanArray",$deliveryChallanArray);
  $smarty->assign("deliveryDetailArray",$deliveryDetailArray);
  $smarty->assign("pendingDCArray",$pendingDCArray);
  $smarty->assign("pendingDCDetailArray",$pendingDCDetailArray);
  if($status == 'C')
    $smarty->display("masterDeliveryListAj2.tpl");
  else
    $smarty->display("masterDeliveryListAj.tpl");
}
?>